package com.AAL.DAO;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IRegionDAO;
import com.AAL.Model.ProductsInStock;
import com.AAL.Model.Region;
import com.AAL.Utils.HibernateUtil;

public class RegionDAO implements IRegionDAO {

	@Override
	public Region getRegion(int regionID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Region region = (Region) session.load(Region.class, regionID);
		return region;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Region> getALlList() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Region> lstRegions = null;
		try {
			lstRegions = (List<Region>) session.createQuery("from Region")
					.list();			
			System.out.println("getAllRegion");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstRegions;
	}
//phat
	@SuppressWarnings("unchecked")
	@Override
	public List<Region> getLstRegionByRegionID(short regionID, short regionID1) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		List<Region> listRegion = null;
		  try {
		   session.beginTransaction();
		   listRegion = (List<Region>) session
		     .createQuery("from Region where regionID=:regionID or regionID=:regionID1").setParameter("regionID",regionID).setParameter("regionID1",regionID1).list();

		   session.getTransaction().commit();
		  } catch (HibernateException e) {
		   e.printStackTrace();
		   session.getTransaction().rollback();
		  }

		  return listRegion;
	}
//phat end
}
